
<template>
  <div class="index-box" ref="index">
    <div class="backBox">
        <span class="back" @click="back"><i class="el-icon-back"></i>返回</span>
    </div>
    <div class="shop-box" v-loading="loading">
      <div class="d-flex">
        <div class="left">
          <el-form  :model="form" size="medium" ref="form"  class="form" :rules="rules" label-position="top">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span class="headerTitle" style="font-weight: bold;">品牌信息</span>
              </div>
              <div class="text item">
                <el-form-item label="品牌logo" prop="brandLogo" >
                  <span slot="label">
                    品牌logo <span class="miniTips">（品牌LOGO为品牌识别标识，具有唯一性，且具有较高识别度，为保证品牌信息准确清晰，请如实上传，图片格式为PNG或JPEG，且尺寸为300(宽)*150(高)px。）</span>
                  </span>
                  <MyUpImg :imagePreview='form.brandLogo'  text='上传品牌logo' @change="imageChange" size="300" imageSize="chang" ></MyUpImg>
                </el-form-item>

                <el-form-item label="" >
                  <div class="d-flex">
                    <el-form-item label="品牌中文名称" prop="brandChineseName" class="flex1">
                      <span slot="label">
                        品牌中文名称 <span class="miniTips">（为避免品牌数据的杂乱、冗余，需要将品牌进行标准化管理，请认真填写。）</span>
                      </span>
                      <el-input v-model="form.brandChineseName" placeholder="请输入品牌中文名称"  />
                    </el-form-item>
                    <el-form-item label="品牌英文名称"  class="flex1" style="padding-left:15px">
                      <span slot="label">
                        品牌英文名称 <span class="miniTips">（为避免品牌数据的杂乱、冗余，需要将品牌进行标准化管理，请认真填写。）</span>
                      </span>
                      <el-input v-model="form.brandEnglishName" placeholder="请输入品牌英文名称"  />
                    </el-form-item>
                  </div>
                </el-form-item>

                <el-form-item label="" >
                  <div class="d-flex">
                    <el-form-item label="首字母" prop="brandNameInitial" class="flex1">
                      <el-input v-model="form.brandNameInitial" placeholder="请输入首字母"  />
                    </el-form-item>
                    <el-form-item label="品牌网站地址"  class="flex1" style="padding-left:15px">
                      <el-input v-model="form.brandWebsiteAddress" placeholder="请输入品牌网站地址"  />
                    </el-form-item>
                  </div>
                </el-form-item>


                <el-form-item label="商品类目" prop="categoryId">
                  <span slot="label">
                    商品类目 <span class="miniTips">（请按照商品属性，将此商品品牌归类在对应的商品类目，有助于商品品牌的统一管理）</span>
                  </span>
                   <treeselect v-model="form.categoryId" :options="deptOptions" :normalizer="normalizer" :show-count="true" placeholder="请选择商品类目" />
                </el-form-item>
                <el-form-item label="品牌说明" prop="brandDescription">
                  <span slot="label">
                    品牌说明 <span class="miniTips">（请用一段剪短的文字，描述该品牌）</span>
                  </span>
                  <el-input v-model="form.brandDescription" placeholder="请输入品牌说明"  />
                </el-form-item>
                <el-form-item label="品牌故事" prop="brandStory">
                  <el-input v-model="form.brandStory" type="textarea" rows="6" placeholder="请用一段文字描述您的品牌故事"  />
                </el-form-item>
                <div v-if="type!='view'" style="display: flex;justify-content: center;">
                  <el-button type="primary" @click="handleSubmit('form')"  :loading="btnLoading" >提 交</el-button>
                </div>
              </div>
            </el-card>
          </el-form>
        </div>
      </div>
     </div>
  </div>
</template>

<script>
  import { goodsBrandInfoAdd,goodsBrandInfoEdit, goodsCategoryList, goodsBrandInfoDetail } from '@/api/getData';
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import common from "@/api/api";

  export default {
    name:'shopDetails',
    components: { Treeselect },
    data() {
      let that = this
      return {
        loading: false,
        btnLoading:false,
        type:'',
        deptOptions:[],
        /*上传图片参数*/
        actionUrl:common.apihost+"/system/file/uploadFile",
        header:{
          AuthorizationMega:''
        },
        form:{
          id:'',
          brandLogo:'',
          brandChineseName:'',
          brandEnglishName:'',
          brandNameInitial:'',
          brandWebsiteAddress:'',
          categoryId:undefined,
          brandDescription:'',
          brandStory:"",
        },
        rules: {
          brandLogo: [{required: true, message: '请上传品牌logo',trigger: 'blur'}],
          brandChineseName: [{required: true, message: '请输入品牌中文名称',trigger: 'blur'}],
          brandNameInitial: [{required: true, message: '请输入首字母',trigger: 'blur'}],
          categoryId: [{required: true, message: '请输入商品类目',trigger: 'blur'}],
        },
      }
    },
    mounted() {
      this.header.AuthorizationMega = localStorage.getItem('bossAppToken');
      this.type = this.$route.query.type;
      this.getTree();
      if(this.$route.query.id){
        this.dataInfo();
      }
    },
    methods: {
      handleSubmit(formName){
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;
            let result;
            if(this.$route.query.id){
              result = await goodsBrandInfoEdit(this.form);
            }else{
              result = await goodsBrandInfoAdd(this.form);
            }
            if (result.code == 200) {
              this.$message.success(result.msg);
              setTimeout(()=>{
                 this.$router.go(-1);
              },3000)
            } else {
              this.$message.error(result.msg);
            }
            this.btnLoading = false;
          }
        })
      },
      async dataInfo(){
        try {
          const result = await goodsBrandInfoDetail({id:this.$route.query.id});
          if (result.code == 200) {
            let data = result.data;
            this.form = {
              id:data.id,
              brandLogo:data.brandLogo,
              brandChineseName:data.brandChineseName,
              brandEnglishName:data.brandEnglishName,
              brandNameInitial:data.brandNameInitial,
              brandWebsiteAddress:data.brandWebsiteAddress,
              categoryId:data.categoryId,
              brandDescription:data.brandDescription,
              brandStory:data.brandStory
            }
          } else {
            this.$message.error(result.msg);
          }
        } catch (err) {
          console.log(err)
        }
      },
      async getTree() {
        try {
          const result = await goodsCategoryList();
          if (result.code == 200) {
            this.deptOptions = result.data
          } else {
            this.$message.error(result.msg);
          }
        } catch (err) {
          console.log(err)
        }
      },
      imageChange(path) {
        this.form.brandLogo = path;
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.categoryId,
          label: node.name,
          children: node.children
        };
      },
      back(){
          this.$router.go(-1);//返回上一层
      },

    }
  }
</script>

<style scoped lang="scss">
  .tableBox /deep/ .el-form-item__content{
    width:100%;
    overflow-x: auto;
  }
  .shop-box /deep/ .el-form-item__label{
      padding:0px;
    }
    .miniTips{
      font-size: 12px;
      color:#CCC4CC;
      padding-left:4px;
    }
    .box-card{
      margin-bottom: 15px;
    }
    .headerTitle{
      font-size: 16px;
      font-weight: bold;
    }
  .table /deep/ .input{
    width: 120px!important;
  }
  .index-box /deep/ .ql-editor{
  	min-height: 300px;
  }
  .boxs /deep/ .el-radio-group{
  	display: flex;
  	flex-wrap: wrap;
  }
  .boxs /deep/ .el-radio-group .el-radio{
  	width:100%;
  	margin-bottom: 10px;
  }
  .activeBox{
    display:flex;
    justify-content:center;
  }
  .upGoodBox{
    height:100px;
    display:flex;
    align-items:center;
    justify-content: center;
    background: rgba(158, 187, 169, 0.15);
    color:#3A7654;
    margin-bottom:20px;
  }
  .shop-box{
    margin: 0 auto;
    .left{
      width:75%;
      padding:0 10px;
      margin: auto;
    }
    .right{
      width:35%;
      padding:0 10px;
    }
  }
  .el-checkbox-group{
    display: flex;
    flex-direction: column;
  }
  .jobNumber{
    text-align:center;
    color:#606266;
    margin:0;
  }
  .index-box /deep/ .el-checkbox{
    margin-bottom:15px;
  }
  .auditBox{
    padding: 20px 50px;
    background: rgba(158, 187, 169, 0.15);
    margin-bottom:20px;
    .steps{
      max-width:500px;
      margin:0 auto;
    }
  }
  .goodSet{
    padding: 50px 50px 30px 50px;
    background: rgba(158, 187, 169, 0.15);
    margin-bottom:20px;
    color:#606266;
    .title{
      text-align: center;
      font-size:18px;
      margin-bottom:10px;
    }
  }
  .el-icon-edit-outline{
    color: rgb(97, 153, 122);
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
  }
  /*.myQuillEditor{
    height:250px;
  }*/
  table {
    width: 100%;
  }

  th {
    height: 50px;
    text-align: left;
    border-bottom: 1px solid #EBEEF5!important;
    color:#909399;
    padding:0 5px;

  }
  table{
    border: 1px solid #EBEEF5;
  }
  td, th{
    border:none;
    white-space: nowrap;
    min-width: 80px;
  }
  td{padding:5px}
  .index-box{
    height:100%;
    overflow-y: auto;
  }
  .shop-box{
    margin: 0 auto;
    .left{
      width:75%;
      padding:0 10px;
    }
    .right{
      width:35%;
      padding:0 10px;
    }
  }
  .rows>div,.rows>button{
    margin-bottom: 5px;
  }
  .input{
    width:180px;
    margin-right:10px;
  }
  .form{
    padding:20px 0;
  }
   .form /deep/ .avatar-uploader .el-upload-list__item{display: none;}
  .avatar-uploader{
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      height:100px;
      width:100px;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
        height: 30px;
        line-height: 30px;
        margin-top: 15px;
        margin-bottom: 10px;
        font-size: 28px;
        color: #8c939d;
    }
    .d-flex /deep/ .el-upload{
      display: block;
    }
    .avatar {
      width: 100px;
      height: 100px;
      display: block;
    }
    .text{
      margin: 0px;
      color: #8c939d;
    }
    .upImg /deep/ .el-upload--text{
      height:100%
    }
    .activeBox{
      display: flex;
      justify-content: center;
    }
    .el-upload-list--picture-card .el-upload-list__item{
        width:100px;
        height:100px;
    }
</style>
